<template>
  <div id="categorylist">
    <van-nav-bar title="分类" left-text="返回" left-arrow @click-left="onClickLeft" class="nav"/>

    <van-tabs v-model="active" class="van-tabs"  @click="openGoods">
      <van-tab
        :title="item.name"
        v-for="item in navList"
        :key="item.id"
        :name="item.id"
        
      >
        <div class="tab-cont">
          <div class="tab-header">
            <p class="h-title">{{item.name}}</p>
            <p class="h-desc">{{item.front_desc}}</p>
          </div>
          <ul class="tab-list">
            <li class="tab-item" v-for="item in goods.data" :key="item.id" @click="toGoodDetail(item.id)">
              <img :src="item.list_pic_url" />
              <p class="item-title">{{item.name}}</p>
              <p class="item-price">￥ {{item.retail_price}}</p>
            </li>
          </ul>
        </div>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import { categoryNav, goodsList } from "@/api/category/categoryList";
export default {
  data() {
    return {
      active: "",
      navList: {},
      goods: [],
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    openGoods() {
      goodsList({ categoryId: this.active }).then((res) => {
      this.goods = res;
    });
    },
    toGoodDetail(val){
      this.$router.push({path:"/goods",query:{id:val}});
    },
  },
  created() {
    categoryNav({ id: this.$route.query.id }).then((res) => {
      this.navList = res.navData;
      this.active = res.currentNav.id;
    });
    goodsList({ categoryId: this.$route.query.id }).then((res) => {
      this.goods = res;
    });
  },
};
</script>

<style lang="scss" scoped>
#categorylist {
  width: 375px;
  height: 100%;
  overflow: auto;
  background-color: rgb(244, 244, 244);
}
.nav {
  position: fixed;
  width: 375px;
  top: 0;
  left: 0;
  z-index: 99;
}
.van-tabs {
  margin-top: 50px;
  .tab-cont {
    margin-top: 8px;
    .tab-header {
      width: 375px;
      height: 100px;
      margin-bottom: 4px;
      background-color: #fff;
      .h-title {
        width: 375px;
        height: 47px;
        padding-top: 32px;
        margin-bottom: 16px;
        font-size: 15px;
        text-align: center;
        background-color: #fff;
        box-sizing: border-box;
      }
      .h-desc {
        width: 375px;
        font-size: 12px;
        text-align: center;
        color: rgb(153, 153, 153);
      }
    }
    .tab-list {
      width: 375px;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      .tab-item {
        width: 185px;
        padding: 10px 17px 0;
        margin-bottom: 4px;
        background-color: #fff;
        box-sizing: border-box;
        img {
          width: 151px;
          height: 151px;
          margin-bottom: 8px;
        }
        .item-title {
          width: 151px;
          margin-bottom: 16px;
          text-align: center;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .item-price {
          width: 151px;
          margin-bottom: 18px;
          font-size: 15px;
          color: rgb(180, 40, 45);
          text-align: center;
        }
      }
    }
  }
}
</style>